[<<Previous Entry]
[^^Up^^]
[Next Entry>>]
[Menu]
[About The Guide]
-iextension
specifies that files processed by the <> construct are
to be edited in-place. It does this by renaming the
input file, opening the output file by the same name,
and selecting that output file as the default for print
statements. The extension, if supplied, is added to
the name of the old file to make a backup copy. If no
extension is supplied, no backup is made. Saying "perl
-p -i.bak -e "s/foo/bar/;" ... " is the same as using
the script:
#!/usr/bin/perl -pi.bak
s/foo/bar/;
which is equivalent to
#!/usr/bin/perl
while (<>) {
if ($ARGV ne $oldargv) {
rename($ARGV, $ARGV . '.bak');
open(ARGVOUT, ">$ARGV");
select(ARGVOUT);
$oldargv = $ARGV;
}
s/foo/bar/;
}
continue {
print; # this prints to original filename
}
select(STDOUT);
except that the -i form doesn't need to compare $ARGV
to $oldargv to know when the filename has changed. It
does, however, use ARGVOUT for the selected filehandle.
Note that STDOUT is restored as the default output
filehandle after the loop.
You can use eof to locate the end of each input file,
in case you want to append to each file, or reset line
numbering (see example under eof).
This page created by ng2html v1.05, the Norton guide to HTML conversion utility.
Written by Dave Pearson